{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "consolidated-helena",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1) + (2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "protective-toolbox",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"2\".isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "decent-recording",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"*\".isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "suspected-birmingham",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'da'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\")da\".lstrip(\")\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 235,
   "id": "whole-judgment",
   "metadata": {},
   "outputs": [],
   "source": [
    "input = \"2-3-4-5\"\n",
    "operators = []\n",
    "nums = []\n",
    "for c in input:\n",
    "    if c.isdigit():\n",
    "        nums.append(c)\n",
    "    else:\n",
    "        operators.append(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 310,
   "id": "voluntary-album",
   "metadata": {},
   "outputs": [],
   "source": [
    "length = len(nums)\n",
    "results_list = []\n",
    "\n",
    "def l_modify(string):\n",
    "    new_string = \"\"\n",
    "    for c in string:\n",
    "        if c == \")\":\n",
    "            pass\n",
    "        elif c == \"(\":\n",
    "            new_string += c\n",
    "        elif c.isdigit():\n",
    "            break\n",
    "    if new_string == \"\":\n",
    "        new_string = \"(\"\n",
    "    return new_string + string.lstrip(\"()\")\n",
    "\n",
    "def r_modify(string):\n",
    "    new_string = \"\"\n",
    "    for c in string[::-1]:\n",
    "        if c == \"(\":\n",
    "            pass\n",
    "        elif c == \")\":\n",
    "            new_string = c + new_string\n",
    "        elif c.isdigit():\n",
    "            break\n",
    "    if new_string == \"\":\n",
    "        new_string = \")\"\n",
    "    return string.rstrip(\"()\") + new_string\n",
    "\n",
    "def modify(lists):\n",
    "    lists[0] = l_modify(lists[0])\n",
    "    lists[-1] = r_modify(lists[-1])\n",
    "    return lists\n",
    "\n",
    "def handle_it(lists, level):\n",
    "    global results_list\n",
    "    if level == length+1:\n",
    "        lists = [modify(l) for l in lists]\n",
    "        results_list = lists.copy()\n",
    "        return\n",
    "        \n",
    "    new_lists = []\n",
    "    for l in lists:\n",
    "        for j in range(length):\n",
    "            the_list = l.copy()\n",
    "            i = j\n",
    "            the_list[i] = \")(\" + the_list[i]\n",
    "            while True:\n",
    "                i += level\n",
    "                if i >= len(the_list):\n",
    "                    the_list[i-level] = \")(\" + the_list[i-level]\n",
    "                    break\n",
    "                the_list[i] = \")(\" + the_list[i]\n",
    "            the_list = [part.replace(\"()\", \"\") for part in the_list]\n",
    "            new_lists.append(the_list.copy())\n",
    "        \n",
    "    handle_it(new_lists.copy(), level+1)\n",
    "    \n",
    "handle_it([nums.copy()], 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 311,
   "id": "neutral-injury",
   "metadata": {},
   "outputs": [],
   "source": [
    "str_list = []\n",
    "for l in results_list:\n",
    "    str_list.append(\"+\".join(l))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 312,
   "id": "little-filing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'(2+)(3+)(4+)(5)', '(2+)(3+4+)(5)', '(2+3+)(4+)(5)', '(2+3+4+)(5)'}"
      ]
     },
     "execution_count": 312,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set(str_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "involved-reputation",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "genetic-leone",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "incorporated-northeast",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "considerable-charles",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 319,
   "id": "chemical-needle",
   "metadata": {},
   "outputs": [],
   "source": [
    "input = \"2-3-4-5-6\"\n",
    "operators = []\n",
    "nums = []\n",
    "for c in input:\n",
    "    if c.isdigit():\n",
    "        nums.append(c)\n",
    "    else:\n",
    "        operators.append(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 320,
   "id": "occupied-shift",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['2', '3', '4', '5', '6']"
      ]
     },
     "execution_count": 320,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 322,
   "id": "resistant-turner",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[], ['2', '3'], ['4', '5', '6']]\n",
      "[['2'], ['3', '4'], ['5', '6']]\n",
      "[['2', '3'], ['4', '5', '6']]\n"
     ]
    }
   ],
   "source": [
    "k = 2\n",
    "for j in range(len(nums)):\n",
    "    if j > k:\n",
    "        break\n",
    "    parts = []\n",
    "    i = j\n",
    "    parts.append(nums[:i])\n",
    "    while True:\n",
    "        i += k\n",
    "        if i >= length:\n",
    "            parts.append(nums[i-k:])\n",
    "            break\n",
    "        parts.append(nums[i-k:i])\n",
    "    print(parts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "returning-concentration",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "delayed-mortality",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 323,
   "id": "respiratory-trout",
   "metadata": {},
   "outputs": [],
   "source": [
    "nums = [0,1,2,3,4,5,6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 324,
   "id": "educated-biodiversity",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1]"
      ]
     },
     "execution_count": 324,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "binary-effect",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "rural-vampire",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['*', '-', '*']\n",
      "[2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "input = \"2*3-4*5\"\n",
    "operators = []\n",
    "nums = []\n",
    "for c in input:\n",
    "    if c.isdigit():\n",
    "        nums.append(int(c))\n",
    "    else:\n",
    "        operators.append(c)\n",
    "print(operators)\n",
    "print(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "alpine-pharmacy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, -14, -10, -10, -14, -34]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = []\n",
    "\n",
    "def handle(operators, nums):\n",
    "    if len(nums) == 1:\n",
    "        results.append(nums[0])\n",
    "        return \n",
    "    j = 0\n",
    "    while True:\n",
    "        new_nums = nums.copy()\n",
    "        new_operators = operators.copy()\n",
    "        operator = new_operators[j]\n",
    "        new_element = None\n",
    "        if operator == \"*\":\n",
    "            new_element = new_nums[j] * new_nums[j+1]\n",
    "        elif operator == \"+\":\n",
    "            new_element = new_nums[j] + new_nums[j+1]\n",
    "        elif operator == \"-\":\n",
    "            new_element = new_nums[j] - new_nums[j+1]\n",
    "        del new_operators[j]\n",
    "        new_nums[j] = new_element\n",
    "        del new_nums[j+1]\n",
    "        #print(nums[j:j+2])\n",
    "        #print(nums[j],nums[j+1], new_operators, new_nums)\n",
    "        handle(new_operators, new_nums)\n",
    "        j += 1\n",
    "        if j >= len(nums)-1:\n",
    "            break\n",
    "            \n",
    "\n",
    "handle(operators, nums)\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "tracked-characterization",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "blessed-damages",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/different-ways-to-add-parentheses/\n",
    "\n",
    "\n",
    "```\n",
    "class Solution:\n",
    "    def diffWaysToCompute(self, input: str) -> List[int]:\n",
    "        results = []\n",
    "        \n",
    "        operators = []\n",
    "        nums = []\n",
    "        for c in input:\n",
    "            if c.isdigit():\n",
    "                nums.append(int(c))\n",
    "            else:\n",
    "                operators.append(c)\n",
    "\n",
    "        def handle(operators, nums):\n",
    "            if len(nums) == 1:\n",
    "                results.append(nums[0])\n",
    "                return \n",
    "            j = 0\n",
    "            while True:\n",
    "                new_nums = nums.copy()\n",
    "                new_operators = operators.copy()\n",
    "                operator = new_operators[j]\n",
    "                new_element = None\n",
    "                if operator == \"*\":\n",
    "                    new_element = new_nums[j] * new_nums[j+1]\n",
    "                elif operator == \"+\":\n",
    "                    new_element = new_nums[j] + new_nums[j+1]\n",
    "                elif operator == \"-\":\n",
    "                    new_element = new_nums[j] - new_nums[j+1]\n",
    "                del new_operators[j]\n",
    "                new_nums[j] = new_element\n",
    "                del new_nums[j+1]\n",
    "                #print(nums[j:j+2])\n",
    "                #print(nums[j],nums[j+1], new_operators, new_nums)\n",
    "                handle(new_operators, new_nums)\n",
    "                j += 1\n",
    "                if j >= len(nums)-1:\n",
    "                    break\n",
    "\n",
    "\n",
    "        handle(operators, nums)\n",
    "        results.sort()\n",
    "        return results\n",
    "```\n",
    "\n",
    "\n",
    "Spent 2 days\n",
    "\n",
    "I think this is a right solution. But time complexity is very high."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "qualified-sauce",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
